<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>08_计算器练习</title>
</head>
<body>
<input type="text" id="i1"><br>
<input type="text" id="i2"><br>
<button onclick="add()">加</button>
<button onclick="sub()">减</button>
<button onclick="ride()">乘</button>
<button onclick="div()">除</button>
<br>
<h4>运算结果:<span id="result"></span></h4>
</body>
<script>
    function add(){
        //获取用户输入的数字
        let a = document.getElementById('i1').value;
        a = parseFloat(a);
        let b = Number(i2.value);
        let result = document.getElementById('result');
        result.innerHTML = a+b;
    }
    function sub(){
        let a = Number(i1.value);
        let b = Number(i2.value);
        //let result = document.getElementById('result');
        let result = document.querySelector('#result');
        result.innerHTML = a-b;
    }
    function ride(){
        let a = Number(i1.value);
        let b = Number(i2.value);
        let result = document.getElementById('result');
        result.innerHTML = a*b;
    }
    function div(){
        let a = Number(i1.value);
        let b = Number(i2.value);
        //let result = document.getElementById('result');
        let result = document.querySelector('span');
        if (b===0){
            alert('除数不能为0!')
        }else{
            result.innerHTML = a/b;
        }
    }
</script>
</html>